package ru.slovoslovo.tzolkin;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.TabHost;

public class DaylyActivity extends FragmentActivity {
    private static final String DAYLY_KIN = "daylyKin";
    private static final String DAYLY_TON = "daylyTon";

    TabHost tHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dayly);

        tHost = (TabHost) findViewById(android.R.id.tabhost);
        tHost.setup();

        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                DaylyKinFragment daylyKinFragment = (DaylyKinFragment) fm.findFragmentByTag(DAYLY_KIN);
                DaylyTonFragment daylyTonFragment = (DaylyTonFragment) fm.findFragmentByTag(DAYLY_TON);
                FragmentTransaction ft = fm.beginTransaction();

                if (daylyKinFragment != null)
                    ft.detach(daylyKinFragment);

                if (daylyTonFragment != null)
                    ft.detach(daylyTonFragment);

                if (tabId.equalsIgnoreCase(DAYLY_KIN)) {

                    if (daylyKinFragment == null) {
                        ft.add(R.id.realtabcontent, new DaylyKinFragment(), DAYLY_KIN);
                    } else {
                        ft.attach(daylyKinFragment);
                    }

                } else {
                    if (daylyTonFragment == null) {
                        ft.add(R.id.realtabcontent, new DaylyTonFragment(), DAYLY_TON);
                    } else {
                        ft.attach(daylyTonFragment);
                    }
                }
                ft.commit();
            }
        };

        tHost.setOnTabChangedListener(tabChangeListener);

        TabHost.TabSpec tSpecAndroid = tHost.newTabSpec(DAYLY_KIN);
        tSpecAndroid.setIndicator(getString(R.string.kin), getResources().getDrawable(R.drawable.ic_action_search));
        tSpecAndroid.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecAndroid);

        TabHost.TabSpec tSpecApple = tHost.newTabSpec(DAYLY_TON);
        tSpecApple.setIndicator(getString(R.string.ton), getResources().getDrawable(R.drawable.ic_action_search));
        tSpecApple.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecApple);

    }

}
